package com.origin.src.mybatis;

import cn.hutool.core.util.IdUtil;
import com.origin.src.ApplicationTool;
import com.origin.src.mybatis.mapper.TUserMapper;
import com.origin.src.mybatis.pojo.TUser;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

public class ShardingTest {


    @Test
    public void shardingTest() throws InterruptedException {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyBatisApp.class);
        ApplicationTool.printBean(app);
        TUserMapper tUserMapper = app.getBean(TUserMapper.class);
        for (int i = 0; i < 100; i++) {
            TUser user = newUser();
            tUserMapper.insert(user);
            if (i % 100 == 0) {
                Thread.sleep(10);
            }
        }
        List<TUser> list = tUserMapper.selectAll();

        for (TUser tUser : list) {
            System.out.println(tUser);
        }
    }

    @Test
    public void shardingTest2() throws InterruptedException {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyBatisApp.class);
        ApplicationTool.printBean(app);
        TUserMapper tUserMapper = app.getBean(TUserMapper.class);
        TUser tUser = newUser();
        tUser.setLoginPassword("明文13667660288");
        tUser.setUserPhone("13238085632");


        List<TUser> list = tUserMapper.selectTest(tUser);

        for (TUser t : list) {
            System.out.println(t);
        }
    }

    @Test
    public void shardingTest3() throws InterruptedException {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyBatisApp.class);
        ApplicationTool.printBean(app);
        TUserMapper tUserMapper = app.getBean(TUserMapper.class);
        List<TUser> list = tUserMapper.selectAll();
        for (TUser t : list) {
            System.out.println(t);
        }
    }


    public TUser newUser() {
        Long id = IdUtil.getSnowflakeNextId();
        TUser tUser = new TUser();
        tUser.setUserId(id);
//        tUser.setUserName(Md5Crypt.md5Crypt(id.toString().getBytes(StandardCharsets.UTF_8)));
        tUser.setUserName("名字" + (id % 65535));
        tUser.setUserEmail(id + "@222.com");
        tUser.setUserPhone(13 + id.toString().substring(10, 19));
        tUser.setLoginPassword("明文" + tUser.getUserPhone());
        return tUser;
    }

}
